package com.ruoyi.enterprise.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.enterprise.domain.SResume;
import com.ruoyi.enterprise.service.ISResumeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 简历信息Controller
 *
 * @author jeffery
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/enterprise/resume")
public class SResumeController extends BaseController
{
    @Autowired
    private ISResumeService sResumeService;

    /**
     * 查询简历信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:list')")
    @GetMapping("/list")
    public TableDataInfo list(SResume sResume)
    {
        startPage();
        List<SResume> list = sResumeService.selectSResumeList(sResume);
        return getDataTable(list);
    }

    /**
     * 导出简历信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:export')")
    @Log(title = "简历信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SResume sResume)
    {
        List<SResume> list = sResumeService.selectSResumeList(sResume);
        ExcelUtil<SResume> util = new ExcelUtil<SResume>(SResume.class);
        return util.exportExcel(list, "简历信息数据");
    }

    /**
     * 获取简历信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:query')")
    @GetMapping(value = "/{resumeId}")
    public AjaxResult getInfo(@PathVariable("resumeId") Long resumeId)
    {
        return AjaxResult.success(sResumeService.selectSResumeByResumeId(resumeId));
    }

    /**
     * 新增简历信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:add')")
    @Log(title = "简历信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SResume sResume)
    {
        return toAjax(sResumeService.insertSResume(sResume));
    }

    /**
     * 修改简历信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:edit')")
    @Log(title = "简历信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SResume sResume)
    {
        return toAjax(sResumeService.updateSResume(sResume));
    }

    /**
     * 删除简历信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:resume:remove')")
    @Log(title = "简历信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{resumeIds}")
    public AjaxResult remove(@PathVariable Long[] resumeIds)
    {
        return toAjax(sResumeService.deleteSResumeByResumeIds(resumeIds));
    }

    @GetMapping(value = "/listAllResumes")
    public AjaxResult listAllResumes()
    {
        return AjaxResult.success(sResumeService.listAllResumes());
    }

    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
